package taller2.utils;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Utils {
	
	// singleton
	private Utils(){}
	
	// lets you add components to the frame in the desired position
	public static void addComponent(JFrame frame, Component component, double x, double y, Point pos) {
		frame.add(component,createConstraints(x,y,pos));
	}	

	// lets you add components to the panel in the desired position
	public static void addComponent(JPanel pannel, Component component, double x, double y, Point pos) {
		pannel.add(component,createConstraints(x,y,pos));
	}	
	
	// creates the constraints for the placement of the object
	private static Object createConstraints(double x, double y, Point pos) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		
		constraints.gridx = (int)pos.getX();
		constraints.gridy = (int)pos.getY();
		
		constraints.weightx = x;
		constraints.weighty = y;
		return constraints;
	}	
	
	// pauses the thread for a cetain amount of time
	public static void sleep(long l){
		try {
			Thread.sleep(l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	// returns a random number between min and max
	public static int randInt(int min, int max) {
	    Random rand = new Random();
	    int randomNum = rand.nextInt((max - min) + 1) + min;

	    return randomNum;
	}	
	
	// returns true if the string is numeric
	public static boolean isANumber(String str){  
		try{  
			Integer.parseInt(str);  
		}catch(NumberFormatException nfe){  
			return false;  
		}  
		return true;  
	}	
	
	// writes a line to a file
	public static void writeLineToFile(String path,String text) {
			try {
				BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path), true));
				bw.write(text);
				bw.newLine();
				bw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	public static int obtenerPos(String tabla, int i, int j,int tam, String separatorStart, String separatorEnd) {
		 int start = getStartingPosition(tabla,matrixPositionToArrayPosition(i,j,tam),separatorStart) + 1;
		 
		 if(start > 0){
			 int end = tabla.indexOf(separatorEnd, start);
			 String number = tabla.substring(start, end);
			 
			 try{
				 int num = Integer.valueOf(number);
				 return num;
			 }catch(NumberFormatException r){
				 return -1;
			 }
		 }
		 return -1;
	}	
	
	public static int getStartingPosition(String code, int higlightedWordNumber, String separator) {
		int pos = -1;
		for(int i=0 ; i<higlightedWordNumber ; i++){
			pos = code.indexOf(separator,pos+1);
	   
			if(pos == -1)
				break;
		}
		return pos;
	}

	public static int matrixPositionToArrayPosition(int i, int j, int tam) {
		return ((j+1)+(tam*i));
	}
	
	public static void deleteFiles(String pathDir){
		File toIgnore = new File(pathDir + ".gitignore");
		
		File dir = new File(pathDir);
		File[] files = dir.listFiles();
		
		for(File file : files){
			if(file.compareTo(toIgnore) != 0)
				file.delete();
		}
	}
}
